package pl.edu.pjwstk.kor.database.init;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import pl.edu.pjwstk.kor.model.Category;
import pl.edu.pjwstk.kor.model.Service;

public class ServiceInitalizer extends AbstractDataInitalizer<Service> {
	private final List<Category> categories;

	public ServiceInitalizer(List<Category> categories) {
		this.categories = categories;
	}

	@Override
	public List<Service> initData() throws Exception {
		List<Service> result = new ArrayList<Service>();
		
		for(String serviceWithDescription : FileUtils.readLines(new File("src/main/resources/services.csv"), "UTF8")) {
			String [] split = serviceWithDescription.split(";");
			Service s = new Service();
			s.setServiceName(split[0].trim());
			if(split.length >= 2) {
				s.setServiceDescription(split[1].trim());
			}
			
			double min = random.nextInt(100);
			double delta = random.nextInt(10000);
			
			s.setMinServiceCost(min + delta);
			s.setMaxServiceCost(s.getMinServiceCost() * 1.4);
			
			Category category = selectRandom(categories, random);
			s.setCategory(category);
			category.getServices().add(s);
			
			result.add(s);
		}
		
		return result;
	}

}
